package cn.edu.cqvie.point;

import cn.edu.cqvie.storage.LocalStorage;

/**
 * 移动端网格计算
 */
public class Mobile3E {

    /**
     * 本地存储数据
     */
    public static LocalStorage localStorage;

    public static final double jl_jd = 102834.74258026089786013677476285;
    public static final double jl_wd = 111712.69150641055729984301412873;
    public static final double jd_2km = 0.01944867998710680373454851233723;
    public static final double wd_2km = 0.01790306878323875449480166909757;
    public static final int rect2km = 2000;
    public static final double jd_1m = 0.00000972433999355340186727425616862;
    public static final double wd_1m = 0.00000895153439161937724740083454878;

    public String m_key = null;       //手机号码
    public int m_ltime = 0;           //以1970-1-1 0点计算的上次操作
    public double m_x = 0;
    public double m_y = 0;
    public double m_z = 0;
    public short m_direction = 0;     //方向0-360
    public float m_speed = 0;         //速度
    public byte m_driverstatus = 1;   //司机状态
    public byte m_cartype = 1;        //车辆类型

    public static void main(String[] args) {
        //互联网产业园
        String grid = calcrectIdStr(106.491756, 29.621079);
        //中国联通重庆分公司
        String grid2 = calcrectIdStr(106.497437, 29.620275);


        localStorage.put(grid, "106.491756,29.621079");
        localStorage.put(grid2, "106.491756,29.621079");


        System.out.println(calcrectIdStr(106.491756, 29.621079));
    }

    public Mobile3E() {
        m_key = null;
        m_ltime = 0;
        m_x = 0;
        m_y = 0;
        m_z = 0;
        m_direction = 0;
        m_speed = 0;
    }

    public Mobile3E(String key, String s) {
        if (s == null || key == null) {
            return;
        }
        String[] ss = s.split(",");
        m_key = key;
        m_x = Double.parseDouble(ss[0]);
        m_y = Double.parseDouble(ss[1]);
        m_z = Double.parseDouble(ss[2]);
        m_direction = Short.parseShort(ss[3]);
        m_speed = Float.parseFloat(ss[4]);
    }

    /**
     * 获取距离 单位米
     * @param x
     * @param y
     * @return
     */
    public double distance(double x, double y)//米
    {
        x = x - m_x;
        y = y - m_y;
        x = x * jl_jd;
        y = y * jl_wd;

        int ix = (int) x;
        int iy = (int) y;

        if (ix == 0 && iy == 0) return 0;

        return Math.sqrt(ix * ix + iy * iy);

    }

    /**
     * 计算两个经度之间的距离（单位：米）
     *
     * @param dx 起点经度
     * @param dy 起点纬度
     * @param sx 终点经度
     * @param sy 终点纬度
     * @return
     */
    public static double distance(double dx, double dy, double sx, double sy)//米
    {
        dx = dx - sx;
        dy = dy - sy;
        dx = dx * jl_jd;
        dy = dy * jl_wd;

        return Math.sqrt(dx * dx + dy * dy);

    }

    /**
     * 计算 x 轴
     *
     * @return
     */
    public static int calcrectXId(double x) {
        return (int) (x / jd_2km);
    }

    /**
     * 计算 y 轴
     *
     * @return
     */
    public static int calcrectYId(double y) {
        return (int) (y / wd_2km);
    }

    /**
     * 计算网格格点
     *
     * @param x 经度
     * @param y 经度
     * @return
     */
    public static String calcrectIdStr(double x, double y) {
        return "rid-" + calcrectXId(x) + "-" + calcrectYId(y);
    }

    /**
     * 获取矩形网格格点
     *
     * @param x 经度
     * @param y 纬度
     * @return 格点
     */
    public static String getRectIdStr(int x, int y) {
        return "rid-" + x + "-" + y;
    }

    /**
     * 计算网点字符串
     *
     * @return
     */
    public String calcrectIdStr() {
        return calcrectIdStr(m_x, m_y);
    }

    /**
     * 计算 x 轴
     *
     * @return
     */
    public int calcrectXId() {
        return calcrectXId(m_x);
    }

    /**
     * 计算 y 轴
     *
     * @return
     */
    public int calcrectYId() {
        return calcrectYId(m_y);
    }
}
